package com.example.tioWsServer;

import com.example.common.WsServerConst;
import com.example.tioWsServer.handler.TioWsServerAioHandler;
import com.example.tioWsServer.handler.WsServerHandlerBase;
import com.example.tioWsServer.listener.TioWsServerAioListener;
import com.example.tioWsServer.listener.WsServerListenerBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.HttpResponse;
import org.tio.server.ServerTioConfig;
import org.tio.server.TioServer;
import org.tio.websocket.common.WsRequest;
import org.tio.websocket.server.WsServerAioHandler;
import org.tio.websocket.server.WsServerAioListener;
import org.tio.websocket.server.WsServerConfig;
import org.tio.websocket.server.WsServerStarter;
import org.tio.websocket.server.handler.IWsMsgHandler;

import java.io.IOException;

/**
 * websocket 服务端
 */
public class WSServerStarter {

    private Logger logger= LoggerFactory.getLogger(WSServerStarter.class);

    private  int port= WsServerConst.PORT;

    private WsServerStarter wsServerStarter;

    private ServerTioConfig serverTioConfig;


    public WSServerStarter(){

    }
    public WSServerStarter(int serverPort){
        this.port=serverPort;
    }

    public void setServerListener(WsServerHandlerBase handler)   {
        try {
            wsServerStarter = new WsServerStarter(this.port, new TioWsServerAioHandler(handler));
            serverTioConfig= wsServerStarter.getServerTioConfig();
            serverTioConfig.setServerAioListener(new WsServerAioListener());
            serverTioConfig.setHeartbeatTimeout(WsServerConst.TIMEOUT);
        } catch (IOException e) {
            logger.error("ws监听异常：{}",e);
        }

    }
    public void setServerListener(WsServerHandlerBase handler, long heartbeatTimeout) {
        try {
            wsServerStarter = new WsServerStarter(this.port, new TioWsServerAioHandler(handler));
            serverTioConfig= wsServerStarter.getServerTioConfig();
            serverTioConfig.setServerAioListener(new WsServerAioListener());
            serverTioConfig.setHeartbeatTimeout(heartbeatTimeout);
        } catch (IOException e) {
            logger.error("ws监听异常：{}",e);
        }
    }

    public void setServerListener(WsServerHandlerBase handler, WsServerListenerBase listener) {
        try {
            wsServerStarter = new WsServerStarter(this.port, new TioWsServerAioHandler(handler));
            serverTioConfig= wsServerStarter.getServerTioConfig();
            serverTioConfig.setServerAioListener(new TioWsServerAioListener(listener));
            serverTioConfig.setHeartbeatTimeout(WsServerConst.TIMEOUT);
        } catch (IOException e) {
            logger.error("ws监听异常：{}",e);
        }

    }

    public void setServerListener(WsServerHandlerBase handler, WsServerListenerBase listener, long heartbeatTimeout) {
        try {
            wsServerStarter = new WsServerStarter(this.port, new TioWsServerAioHandler(handler));
            serverTioConfig= wsServerStarter.getServerTioConfig();
            serverTioConfig.setServerAioListener(new TioWsServerAioListener(listener));
            serverTioConfig.setHeartbeatTimeout(heartbeatTimeout);
        } catch (IOException e) {
            logger.error("ws监听异常：{}",e);
        }
    }

    public void start(){
        try {
            if(wsServerStarter!=null){
                wsServerStarter.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
